class TodosController < ApplicationController
#  before_filter :require_no_user, :only => [:new, :create]
  before_filter :require_user, :only => [:index, :list, :show, :new, :edit, :create, :update, :destroy]

  # GET /todos
  # GET /todos.xml
  def index
#    @todos = Todo.all
#
#    respond_to do |format|
#      format.html # index.html.erb
#      format.xml  { render :xml => @todos }
#    end
    list
    render :action => 'list'
  end

  def list
#    @todos = Todo.all

    if params[:page] == nil then
      @todos = Todo.paginate_by_user_id @current_user.id, :page => 1
    else
      @todos = Todo.paginate_by_user_id @current_user.id, :page => params[:page]
    end
  end

  # GET /todos/1
  # GET /todos/1.xml
  def show
    @todo = Todo.find_by_id(params[:id])
    unless @todo.briefcase_file_name == nil then
      @app_url_of_briefcase = request.protocol + request.host_with_port+ @todo.briefcase.url
    else
      @app_url_of_briefcase = nil
    end
    
      if @todo == nil then
        list
        render :action => 'list'
      end
    
#    respond_to do |format|
#      format.html # show.html.erb
#      format.xml  { render :xml => @todo }
#    end
  end

  # GET /todos/new
  # GET /todos/new.xml
  def new
    @todo = Todo.new

#    respond_to do |format|
#      format.html # new.html.erb
#      format.xml  { render :xml => @todo }
#    end
  end

  # GET /todos/1/edit
  def edit
    @todo = Todo.find_by_id(params[:id])
     if @todo == nil then
        list
        render :action => 'list'
     end

  end

  # POST /todos
  # POST /todos.xml
  def create
    @todo = Todo.new(params[:todo])
    @todo.status = 0; # 0: undone 1: done
    @todo.user_id = @current_user.id
    if @todo.save
      flash[:notice] = 'Product was successfully created.'
      list
      render :action => 'list'
    else
      render :action => 'new'
    end
#    respond_to do |format|
#      if @todo.save
#        flash[:notice] = 'Todo was successfully created.'
#        format.html { redirect_to(@todo) }
#        format.xml  { render :xml => @todo, :status => :created, :location => @todo }
#      else
#        format.html { render :action => "new" }
#        format.xml  { render :xml => @todo.errors, :status => :unprocessable_entity }
#      end
#    end
  end

  # PUT /todos/1
  # PUT /todos/1.xml
  def update
    @todo = Todo.find_by_id(params[:id])
     if @todo == nil then
        list
        render :action => 'list'
     else

        if @todo.update_attributes(params[:todo])
          flash[:notice] = 'Product was successfully updated.'
          redirect_to :action => 'show', :id => @todo
        else
          render :action => 'edit'
        end
     end

#    respond_to do |format|
#      if @todo.update_attributes(params[:todo])
#        flash[:notice] = 'Todo was successfully updated.'
#        format.html { redirect_to(@todo) }
#        format.xml  { head :ok }
#      else
#        format.html { render :action => "edit" }
#        format.xml  { render :xml => @todo.errors, :status => :unprocessable_entity }
#      end
#    end
  end

  # DELETE /todos/1
  # DELETE /todos/1.xml
  def destroy
    @todo = Todo.find_by_id(params[:id])
     if @todo != nil then
        @todo.destroy
     end
     list
     render :action => 'list'
    
#    respond_to do |format|
#      format.html { redirect_to(todos_url) }
#      format.xml  { head :ok }
#    end
  end
end
